<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Modules
 * @package    User
 * @subpackage Forms
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Login form
 *
 * @category   LOSC Modules
 * @package    User
 * @subpackage Forms
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class User_Form_Login extends Zend_Form
{
    const REGEX_USERNAME = '/^[a-z][a-z0-9@.\-_]*$/';
    
    /**
     * Creates a login form
     * 
     * @return void
     */
    public function init()
    {
        // call parent for good measure
        parent::init();
        
        // default url
        $url = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
        $url = $url->url(array(
            'module'     => 'user',
            'controller' => 'login',
            'action'     => 'process'
        ), 'default', true);
        
        // set misc stuff
        $this->setMethod('post')
             ->setAction($url)
             ->setAttrib('id', 'login-form');
        
        // create elements
        $username = new Zend_Form_Element_Text('username');
        $password = new Zend_Form_Element_Password('password');
        $remember = new Zend_Form_Element_Checkbox('remember');
        $submit   = new Zend_Form_Element_Submit('login');
        
        // add to form
        $this->addElement($username);
        $this->addElement($password);
        $this->addElement($remember);
        $this->addElement($submit);
        
        $username->setRequired(true)
                 ->setLabel('Username / email')
                 ->setAttrib('class', 'text')
                 ->setAttrib('size', '32')
                 ->addFilters(array('StringTrim', 'StringToLower'))
                 ->addValidators(array(
                     array('NotEmpty', true),
                     array('StringLength', true, array(3, 64)),
                     array('regex', true, array(self::REGEX_USERNAME))
                 ));
        
        $password->setRequired(true)
                 ->setLabel('Password')
                 ->setAttrib('class', 'text')
                 ->setAttrib('size', '32')
                 ->addValidators(array(
                     array('NotEmpty', true),
                     array('StringLength', true, array(4, 64)),
                 ));
        
        $remember->setLabel('remember me on this computer')
                 ->setAttrib('class', 'checkbox')
                 ->addFilters(array('StringTrim', 'StringToLower'))
                 ->setDecorators(array(
                     array('ViewHelper', array('helper' => 'formCheckbox')),
                     array('Errors'),
                     array('Label', array('tag' => 'span', 'placement' => 'APPEND')),
                     array('DtDdWrapper')
                 ));
        
        $submit->setLabel('Sign in')
               ->setAttrib('class', 'button');
    }
    
    public function render(Zend_View_Interface $view = null)
    {
        return str_replace('<dt>&nbsp;</dt>', '<dt></dt>', parent::render($view));
    }
}
